package yxy.game.pm2.command.yilingbattle;

import yxy.cherry.battle.module.record.result.BattleResult;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.YiLingMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.battle.BattleModule;
import yxy.game.pm2.module.yiling.YiLingBattleHandle;
import yxy.game.pm2.module.yiling.YiLingBattleModule;

/****************************************
 * @name CMD_YiLingBattleEnemyBattleClear
 * @description 夷陵之战 征战结算
 * @author 梁健彬
 * @since 2022-05-17
 ***************************************/
@MessageType(proto = Command.YiLingBattleEnemyBattleClear, classtype = YiLingMessage.YiLingBattleEnemyBattleClearRequest.class)
public class CMD_YiLingBattleEnemyBattleClear extends Command<YiLingMessage.YiLingBattleEnemyBattleClearRequest> {

    @Override
    public void execute(MessageSource messageSource, YiLingMessage.YiLingBattleEnemyBattleClearRequest request) {

        YiLingMessage.YiLingBattleEnemyBattleClearResponse.Builder response = YiLingMessage.YiLingBattleEnemyBattleClearResponse.newBuilder();
        try {

            String playerCode = messageSource.getTag();
            String battleCode = request.getBattleCode();

            BattleModule battleModule = BattleModule.get(playerCode, battleCode);

            if(battleModule == null){
                send(messageSource, response, Result.DataError);//
                return;
            }

            if (battleModule.isSettled()) {
                send(messageSource, response, Result.BattleHasSettled);// 战斗已结算
                return;
            }
            if (!battleModule.isFinished()) {
                send(messageSource, response, Result.BattleIsNotFinished);// 战斗尚未结束
                return;
            }

            YiLingBattleHandle handle = new YiLingBattleHandle();
            if (handle.isBattling(battleModule)) {
                if (!battleModule.isSkip()) {
                    send(messageSource, response, Result.BattleIsNotFinished);// 战斗尚未结束
                    return;
                }
            }

            response.setInfo(new BattleHandle().battleClear(battleModule));
            //设置战斗结算
            battleModule.setSettled(true);

            BattleResult battleResult = battleModule.getBattleResult();
            if (!battleResult.equals(BattleResult.LEFT_WIN)) {
                battleModule.set(YiLingBattleHandle.battleExpireDay);
                send(messageSource, response, Result.Success);
                return;
            }

            //获胜进行结算处理
            battleModule.getSettlement().settle();
            battleModule.set(YiLingBattleHandle.battleExpireDay);

            YiLingBattleModule yiLingBattleModule = YiLingBattleModule.get(playerCode);
            int mapId = yiLingBattleModule.getMapId();
            PlayerBean player = PlayerBean.get(playerCode);
            response.setMapId(mapId);
            response.setLevel(player.getLevel());
            response.setExp(Currency.number(playerCode, CurrencyType.CT_主公经验, 0,false));
            response.addAllRewards(new CurrencyHandle().packItems(battleModule.getSettlement().reward()));

            send(messageSource, response, Result.Success);

        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);

        }

    }

    private void send(MessageSource messageSource, YiLingMessage.YiLingBattleEnemyBattleClearResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}